package net.lainiao;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.nodes.PersistentEphemeralNode;
import org.apache.curator.framework.state.ConnectionState;
import org.apache.curator.framework.state.ConnectionStateListener;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.test.KillSession;
import org.apache.curator.test.TestingServer;
import org.apache.curator.utils.CloseableUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 *创建临时性节点和持久化节点
 */
public class PersistentEphemeralNodeExample {
    //    private static final String PATH = "/example/ephemeralNode";
//    private static final String PATH2 = "/example/node";

    public static void main(String[] args) throws Exception {
        String temNodePath="/text1/temNode";
        String proNodePath="/text1/proNode";
        TestingServer server = new TestingServer();
        CuratorFramework client = null;
        PersistentEphemeralNode node = null;
        try {
            client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(1000, 3));
            client.getConnectionStateListenable().addListener(new ConnectionStateListener() {
                @Override
                public void stateChanged(CuratorFramework client, ConnectionState newState) {
                    System.out.println("客户端状态:" + newState.name());
                }
            });
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date=new Date();
            String dateStr=simpleDateFormat.format(date);
            client.start();
            System.out.println("创建临时节点");
            PersistentEphemeralNode temNode=new PersistentEphemeralNode(client, PersistentEphemeralNode.Mode.EPHEMERAL,temNodePath, "temNodeValue".getBytes());
            temNode.start();
            temNode.waitForInitialCreate(3, TimeUnit.SECONDS);
            String actualPath = temNode.getActualPath();
            String temNodeValue=new String(client.getData().forPath(actualPath));
            System.out.println("temNodeVlaue=="+temNodeValue);

            System.out.println("创建持久化节点");
            client.create().forPath(proNodePath,("proNodeValue"+dateStr).getBytes());
            String proNodeValue=new String(client.getData().forPath(proNodePath));
            System.out.println("proNodeValue=="+proNodeValue);

            System.out.println("关闭客户端会话");
            KillSession.kill(client.getZookeeperClient().getZooKeeper(), server.getConnectString());

            System.out.println("查看临时节点是否存在");
            Object  temNodeStatuObj=client.checkExists().forPath(temNodePath);
            if(temNodeStatuObj==null){
                System.out.println("临时节点不存在");
            }
            else{
                System.out.println("临时节点存在:"+temNodeStatuObj.toString());
            }

            System.out.println("查看永久节点是否存在");
            System.out.println(client.checkExists().forPath(proNodePath));

            System.out.println("获取永久节点的值");
            System.out.println("proNodeValue="+new String(client.getData().forPath(proNodePath)));


        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            CloseableUtils.closeQuietly(node);
            CloseableUtils.closeQuietly(client);
            CloseableUtils.closeQuietly(server);
        }

    }

}
